package com.qms.repast.ui.presenter;

import android.app.Activity;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.qms.repast.App;
import com.qms.repast.commons.Constants;
import com.qms.repast.db.CuisineOrderBeanDao;
import com.qms.repast.entity.reqbean.ReqFood;
import com.qms.repast.entity.resbean.CuisineBean;
import com.qms.repast.entity.resbean.CuisineMenuBean;
import com.qms.repast.entity.resbean.CuisineOrderBean;
import com.qms.repast.entity.resbean.FoodBean;
import com.qms.repast.entity.resbean.FoodBean.BodyBean.RowsBean.ItemsBean;
import com.qms.repast.module.UserModule;
import com.qms.repast.ui.base.BasePresenter;
import com.qms.repast.ui.view.ICuisineView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.observers.DisposableObserver;

/**
 * @author 泅渡者
 * @date 2018/9/15 0015
 * @describe:
 */
public class CuisinePresenter extends BasePresenter<ICuisineView> {

	private UserModule userModule;

	public CuisinePresenter (Activity mContext, ICuisineView mView) {
		super(mContext, mView);
		this.userModule = new UserModule(mContext);
	}

	/**
	 * 获取菜单数据
	 */
	public void getFoodList (ReqFood reqFood) {
		userModule.reqFoodList(reqFood, new DisposableObserver<FoodBean>() {
			@Override
			public void onError (Throwable throwable) {
				Logger.e(throwable.getMessage());
			}

			@Override
			public void onComplete () {
				Logger.d("Request is End");
			}

			@Override
			public void onNext (FoodBean foodBean) {
				Logger.d(new Gson().toJson(foodBean));
				if (Constants.SUCCESS == foodBean.getCode()) {
					//待入库菜品数据集合
					List<CuisineBean> cuisineBeans = new ArrayList<>();
					//待入库菜品类别数据集合
					List<CuisineMenuBean> cuisineMenuBeans = new ArrayList<>();
					int rows = foodBean.getBody().getRows().size();
					for (int i = 0; i < rows; i++) {
						//遍历类别 并入库
						FoodBean.BodyBean.RowsBean rowsBean = foodBean.getBody().getRows().get(i);
						CuisineMenuBean cuisineMenuBean = new CuisineMenuBean();
						cuisineMenuBean.setCategoryId(rowsBean.getCategoryId());
						cuisineMenuBean.setCategoryTitle(rowsBean.getCategoryTitle());
						cuisineMenuBeans.add(cuisineMenuBean);
						int nums = foodBean.getBody().getRows().get(i).getItems().size();
						for (int y = 0; y < nums; y++) {
							//遍历菜品 并入库
							ItemsBean itemsBean = foodBean.getBody().getRows().get(i).getItems().get(y);
							CuisineBean cuisineBean = new CuisineBean();
							cuisineBean.setItemId(itemsBean.getItemId());
							cuisineBean.setPriceId(itemsBean.getPriceId());
							cuisineBean.setAssembleType(itemsBean.getAssembleType());
							cuisineBean.setCategoryId(itemsBean.getCategoryId());
							cuisineBean.setPrice(itemsBean.getPrice());
							cuisineBean.setCategoryTitle(itemsBean.getCategoryTitle());
							cuisineBean.setFoodType(itemsBean.getFoodType());
							cuisineBean.setIcon(itemsBean.getIcon());
							cuisineBean.setIsPeriodPrice(itemsBean.getIsPeriodPrice());
							cuisineBean.setIsRecommend(itemsBean.getIsRecommend());
							cuisineBean.setIsRequired(itemsBean.getIsRequired());
							cuisineBean.setIsSku(itemsBean.getIsSku());
							cuisineBean.setIsWeigh(itemsBean.getIsWeigh());
							cuisineBean.setOptNum(0);
							cuisineBean.setItemType(itemsBean.getItemType());
							cuisineBean.setOriginalPrice(itemsBean.getOriginalPrice());
							cuisineBean.setPepperLevel(itemsBean.getPepperLevel());
							cuisineBean.setPrice(itemsBean.getPrice());
							cuisineBean.setStock(itemsBean.getStock());
							cuisineBean.setStockWarn(itemsBean.getStockWarn());
							cuisineBean.setTitle(itemsBean.getTitle());
							cuisineBean.setUnit(itemsBean.getUnit());
							cuisineBeans.add(cuisineBean);
						}
					}
					App.getmDaoSession().getCuisineBeanDao().insertOrReplaceInTx(cuisineBeans);
					App.getmDaoSession().getCuisineMenuBeanDao().insertOrReplaceInTx(cuisineMenuBeans);
					mView.showUi();
				}
			}
		});

	}

	/**
	 * @param userId
	 * @param shopId
	 */
	public void selectItemCount (String userId, String shopId) {
		//在购物车中查找该菜品
		List<CuisineOrderBean> cuisineOrderBeans = App.getmDaoSession()
				.getCuisineOrderBeanDao()
				.queryBuilder()
				.where(
						CuisineOrderBeanDao.Properties.UserId.eq(userId),
						CuisineOrderBeanDao.Properties.ShopId.eq(shopId)
				).list();
		mView.showItemCount(cuisineOrderBeans.size());
	}
}
